<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>counter</title>
</head>
<body>
    <p id="content">0</p>
    <button id="switch">开始</button>

        <script>
            if(typeof(Worker) !=="undefined"){//判断work是否定义

            }
            else {

            }
            let w =new Worker('./counter.js');



            document.getElementById('switch').onclick = function () {

                if(document.getElementById('switch').textContent == "开始") {
                    w.onmessage = function (event) {
                        document.getElementById('content').textContent = event.data;
                    }
                    document.getElementById('switch').textContent = "暂停";
                }
                else {
                    w.terminate();
                    w = undefined;
                    document.getElementById('switch').textContent = "开始";
                }

            }

        </script>





</body>
</html>